﻿@{
    //  设置布局页和页标题
    Layout = "~/_SiteLayout.cshtml";
    Page.Title = "密码重置";

    var newPasswordError = "";
    var confirmPasswordError = "";
    var passwordResetTokenError = "";
    var passwordResetToken = Request.Form["resetToken"] ?? Request.QueryString["resetToken"];
    var disabledAttribute = "";

    bool isValid = true;
    bool tokenExpired = false;
    bool isSuccess = false;

    if (IsPost) {
        var newPassword = Request["newPassword"];
        var confirmPassword = Request["confirmPassword"];

        if (newPassword.IsEmpty()) {
            newPasswordError = "请输入新密码。";
            isValid = false;
        }

        if (confirmPassword != newPassword) {
            confirmPasswordError = "确认密码和新密码不匹配。";
            isValid = false;
        }

        if (passwordResetToken.IsEmpty()) {
            passwordResetTokenError = "请输入密码重置令牌。 它应该已通过电子邮件发送给您。";
            isValid = false;
        }

        if (isValid) {
            if (WebSecurity.ResetPassword(passwordResetToken, newPassword)) {   
                disabledAttribute = @"disabled=""disabled""";
                isSuccess = true;
            } else {
                passwordResetTokenError = "密码重置令牌无效。";
                tokenExpired = true;
                isValid = false;
            }
        } else {
            isValid = false;
        }
    }
}

@if (!isValid) {
    <p class="message error">
        @if (tokenExpired) {
            <text>密码重置令牌错误或可能已过期。 请访问<a href="@Href("~/Account/ForgotPassword")">忘记密码页</a>，生成新令牌。</text>
        } else {
            <text>无法重置密码。 请更正错误并重试。</text>
        }
    </p>
}

@if (isSuccess) {
    <p class="message success">
        密码已更改!  单击<a href="@Href("~/Account/Login")" title="登录">此处</a> 登录。
    </p>
}

<form method="post" action="">
    <fieldset>
        <legend>密码更改表单</legend>
        @if (!WebMail.SmtpServer.IsEmpty()) {
            <p>请在下方键入新密码并单击<em>重置密码</em>按钮以更改密码。</p>
            <ol>
                <li class="new-password">
                    <label for="newPassword">新密码:</label> 
                    <input type="password" id="newPassword" name="newPassword" title="新密码" @disabledAttribute @if(!newPasswordError.IsEmpty()){<text>class="error-field"</text>} />
                    @if (!newPasswordError.IsEmpty()) {
                        <label for="newPassword" class="validation-error">@newPasswordError</label>
                    }
                </li>
                <li class="confirm-password">
                    <label for="confirmPassword">确认密码:</label> 
                    <input type="password" id="confirmPassword" name="confirmPassword" title="确认新密码" @disabledAttribute @if(!confirmPasswordError.IsEmpty()){<text>class="error-field"</text>} />
                    @if (!confirmPasswordError.IsEmpty()) {
                        <label for="confirmPassword" class="validation-error">@confirmPasswordError</label>
                    }
                </li>
                <li class="reset-token">
                    <label for="resetToken">密码重置令牌:</label> 
                    <input type="text" id="resetToken" name="resetToken" value="@passwordResetToken" title="密码重置令牌" @disabledAttribute @if(!passwordResetTokenError.IsEmpty()){<text>class="error-field"</text>} />
                    @if (!passwordResetTokenError.IsEmpty()) {
                        <label for="resetToken" class="validation-error">@passwordResetTokenError</label>
                    }
                </li>
            </ol>
            <p class="form-actions">
                <input type="submit" value="Reset Password" title="重置密码" @disabledAttribute/>
            </p>
        } else {
            <p class="message info">
                因为没有正确配置 SMTP 服务器，所以此网站禁用了 
                密码恢复功能。 要重置密码，请与此站点的所有者 
                联系。
            </p>
        }
    </fieldset>
</form>

